*** ppmtomitsu.c.orig	Wed Mar 31 12:24:24 1993
--- ppmtomitsu.c	Thu Jul 08 19:48:53 1993
***************
*** 3,8 ****
--- 3,13 ----
  **                (or the S3410-30 parallel interface)
  **
  ** Copyright (C) 1992,93 by S.Petra Zeidler
+ ** Minor modifications by Ingo Wilken:
+ **  - moved large arrays off the stack into globals (A_SMALLSTACK)
+ **  - mymalloc() and check_and_rotate() functions for often used
+ **    code fragments.  Reduces code size by a few KB.
+ **  - use pm_error() instead of fprints(stderr)
  **
  ** This software was written for the Max Planck Institut fuer Radioastronomie,
  ** Bonn, Germany, Optical Interferometry group
***************
*** 19,27 ****
--- 24,41 ----
  #include "ppm.h"
  #include "ppmcmap.h"
  #include "pbmplus.h"
+ #ifdef __STDC__
+ #include <stdlib.h>
+ #else
  #include <memory.h>
+ #endif
  #include "mitsu.h"
  
+ #ifdef A_SMALLSTACK
+ hashinfo colorhashtable[HASHSIZE];
+ #endif
+ 
+ 
  #ifdef __STDC__
  void main(int argc, char *argv[] )
  #else
***************
*** 31,37 ****
--- 45,53 ----
  #endif
      {
      FILE             *ifp;
+ #ifndef A_SMALLSTACK
      hashinfo         colorhashtable[HASHSIZE];
+ #endif
      struct hashinfo  *hashrun;
      pixel            *xP;
      int              argn;
***************
*** 70,76 ****
          }
      else if (pm_keymatch(argv[argn], "-media", 2)) {
          ++argn;
!         if (argn == argc || sscanf(argv[argn], "%s", media) < 1)
              pm_usage(usage);
          else if (mytoupper(media[0]) != 'A')
              pm_usage(usage);
--- 86,92 ----
          }
      else if (pm_keymatch(argv[argn], "-media", 2)) {
          ++argn;
!         if (argn == argc || sscanf(argv[argn], "%15s", media) < 1)
              pm_usage(usage);
          else if (mytoupper(media[0]) != 'A')
              pm_usage(usage);
***************
*** 130,147 ****
  
          ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
          pixelrow = (pixel *) ppm_allocrow(cols);
!         if ((redrow = malloc(cols * sizeof(char))) == NULL) {
!             fprintf(stderr, "ppmtomitsu: malloc failed, exiting\n");
!             exit(1);
!         }
!         if ((greenrow = malloc( cols * sizeof(char))) == NULL) {
!             fprintf(stderr, "ppmtomitsu: malloc failed, exiting\n");
!             exit(1);
!         }
!         if ((bluerow = malloc( cols * sizeof(char))) == NULL) {
!             fprintf(stderr, "ppmtomitsu: malloc failed, exiting\n");
!             exit(1);
!         }
          lineputinit(cols, rows, sharpness, enlarge, copy, medias);
  
          for ( row = 0; row < rows; ++row ) {
--- 146,154 ----
  
          ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
          pixelrow = (pixel *) ppm_allocrow(cols);
!         redrow = mymalloc(cols * sizeof(char));
!         greenrow = mymalloc(cols * sizeof(char));
!         bluerow = mymalloc(cols * sizeof(char));
          lineputinit(cols, rows, sharpness, enlarge, copy, medias);
  
          for ( row = 0; row < rows; ++row ) {
***************
*** 215,227 ****
                      else {
                          while (hashrun->next != NULL)
                              hashrun = hashrun->next;
!                         if ((hashrun->next = 
! 						(struct hashinfo *) malloc(sizeof(struct hashinfo)))
!                                                                 == NULL) {
!                             fprintf(stderr, "%s: malloc failed, exiting\n",
!                                 argv[0]);
!                             exit(2);
!                         }
                          hashrun = hashrun->next;
                          hashrun->color = (table[colval]).color;
                          hashrun->flag  = colval;
--- 222,229 ----
                      else {
                          while (hashrun->next != NULL)
                              hashrun = hashrun->next;
!                         hashrun->next =
!                             (struct hashinfo *) mymalloc(sizeof(struct hashinfo));
                          hashrun = hashrun->next;
                          hashrun->color = (table[colval]).color;
                          hashrun->flag  = colval;
***************
*** 246,258 ****
                      else {
                          while (hashrun->next != NULL)
                              hashrun = hashrun->next;
!                         if ((hashrun->next = 
! 						(struct hashinfo *) malloc(sizeof(struct hashinfo)))
!                                                                 == NULL) {
!                             fprintf(stderr, "%s: malloc failed, exiting\n",
!                                 argv[0]);
!                             exit(2);
!                         }
                          hashrun = hashrun->next;
                          hashrun->color = (table[colval]).color;
                          hashrun->flag  = colval;
--- 248,255 ----
                      else {
                          while (hashrun->next != NULL)
                              hashrun = hashrun->next;
!                         hashrun->next =
!                             (struct hashinfo *) mymalloc(sizeof(struct hashinfo));
                          hashrun = hashrun->next;
                          hashrun->color = (table[colval]).color;
                          hashrun->flag  = colval;
***************
*** 269,277 ****
                          if (hashrun->next != NULL)
                              hashrun = hashrun->next;
                          else {
!                             fprintf(stderr, "%s: you just found a letal bug.\n",
!                                 argv[0]);
!                             exit(1);
                          }
                      datum(hashrun->flag);
                  }
--- 266,272 ----
                          if (hashrun->next != NULL)
                              hashrun = hashrun->next;
                          else {
!                             pm_error("you just found a lethal bug.");
                          }
                      datum(hashrun->flag);
                  }
***************
*** 395,425 ****
      default:
          SHARPNESS(SP_USER);
      }
!     if (cols > rows) {
!         ROTATEIMG(DOROTATE);                        /* rotate image */
!         if (enlarge*rows > medias.maxcols || enlarge*cols > medias.maxrows) {
!             fprintf(stderr, "ppmtomitsu: Image too large, MaxPixels = %d x %d\n"
!                           , medias.maxrows, medias.maxcols);
!             exit(1);
!         }
!         HPIXELS(cols);
!         VPIXELS(rows);
!         HPIXELSOFF((medias.maxcols/enlarge - rows)/2);
!         VPIXELSOFF((medias.maxrows/enlarge - cols)/2);
!         pm_message("rotating image for output");
!     }
!     else {
!         ROTATEIMG(DONTROTATE);
!         if (enlarge*rows > medias.maxrows || enlarge*cols > medias.maxcols) {
!             fprintf(stderr, "ppmtomitsu: Image too large, MaxPixels = %d x %d\n"
!                           , medias.maxrows, medias.maxcols);
!             exit(1);
!         }
!         HPIXELS(cols);
!         VPIXELS(rows);
!         HPIXELSOFF((medias.maxcols/enlarge - cols)/2);
!         VPIXELSOFF((medias.maxrows/enlarge - rows)/2);
!     }
      DATASTART;
      return;
  }
--- 390,396 ----
      default:
          SHARPNESS(SP_USER);
      }
!     check_and_rotate(cols, rows, enlarge, medias);
      DATASTART;
      return;
  }
***************
*** 495,525 ****
  #endif /*__STDC__*/
  {
      DONELOOKUPTABLE;
!     if (cols > rows) {
!         ROTATEIMG(DOROTATE);                        /* rotate image */
!         if (enlarge*rows > medias.maxcols || enlarge*cols > medias.maxrows) {
!             fprintf(stderr, "ppmtomitsu: Image too large, MaxPixels = %d x %d\n"
!                           , medias.maxrows, medias.maxcols);
!             exit(1);
!         }
!         HPIXELS(cols);
!         VPIXELS(rows);
!         HPIXELSOFF((medias.maxcols/enlarge - rows)/2);
!         VPIXELSOFF((medias.maxrows/enlarge - cols)/2);
!         pm_message("rotating image for output");
!     }
!     else {
!         ROTATEIMG(DONTROTATE);
!         if (enlarge*rows > medias.maxrows || enlarge*cols > medias.maxcols) {
!             fprintf(stderr, "ppmtomitsu: Image too large, MaxPixels = %d x %d\n"
!                           , medias.maxrows, medias.maxcols);
!             exit(1);
!         }
!         HPIXELS(cols);
!         VPIXELS(rows);
!         HPIXELSOFF((medias.maxcols/enlarge - cols)/2);
!         VPIXELSOFF((medias.maxrows/enlarge - rows)/2);
!     }
      DATASTART;
      return;
  }
--- 466,472 ----
  #endif /*__STDC__*/
  {
      DONELOOKUPTABLE;
!     check_and_rotate(cols, rows, enlarge, medias);
      DATASTART;
      return;
  }
***************
*** 583,594 ****
      default:
          SHARPNESS(SP_USER);
      }
      if (cols > rows) {
          ROTATEIMG(DOROTATE);                        /* rotate image */
          if (enlarge*rows > medias.maxcols || enlarge*cols > medias.maxrows) {
!             fprintf(stderr, "ppmtomitsu: Image too large, MaxPixels = %d x %d\n"
!                           , medias.maxrows, medias.maxcols);
!             exit(1);
          }
          HPIXELS(cols);
          VPIXELS(rows);
--- 530,573 ----
      default:
          SHARPNESS(SP_USER);
      }
+     check_and_rotate(cols, rows, enlarge, medias);
+     return;
+ }
+ 
+ 
+ #ifdef __STDC__
+ static void *
+ mymalloc(long bytes)
+ #else
+ static char *
+ mymalloc(bytes)
+     long bytes;
+ #endif
+ {
+     void *mem;
+ 
+     mem = malloc(bytes);
+     if( mem == NULL )
+         pm_error("out of memory allocating %d bytes", bytes);
+ 
+     return mem;
+ }
+ 
+ 
+ #ifdef __STDC__
+ static void
+ check_and_rotate(int cols, int rows, int enlarge, struct mediasize medias)
+ #else
+ static void
+ check_and_rotate(cols, rows, enlarge, medias)
+     int cols, rows, enlarge;
+     struct mediasize medias;
+ #endif
+ {
      if (cols > rows) {
          ROTATEIMG(DOROTATE);                        /* rotate image */
          if (enlarge*rows > medias.maxcols || enlarge*cols > medias.maxrows) {
!             pm_error("Image too large, MaxPixels = %d x %d", medias.maxrows, medias.maxcols);
          }
          HPIXELS(cols);
          VPIXELS(rows);
***************
*** 599,607 ****
      else {
          ROTATEIMG(DONTROTATE);
          if (enlarge*rows > medias.maxrows || enlarge*cols > medias.maxcols) {
!             fprintf(stderr, "ppmtomitsu: Image too large, MaxPixels = %d x %d\n"
!                           , medias.maxrows, medias.maxcols);
!             exit(1);
          }
          HPIXELS(cols);
          VPIXELS(rows);
--- 578,584 ----
      else {
          ROTATEIMG(DONTROTATE);
          if (enlarge*rows > medias.maxrows || enlarge*cols > medias.maxcols) {
!             pm_error("Image too large, MaxPixels = %d x %d", medias.maxrows, medias.maxcols);
          }
          HPIXELS(cols);
          VPIXELS(rows);
***************
*** 608,612 ****
          HPIXELSOFF((medias.maxcols/enlarge - cols)/2);
          VPIXELSOFF((medias.maxrows/enlarge - rows)/2);
      }
-     return;
  }
--- 585,589 ----
          HPIXELSOFF((medias.maxcols/enlarge - cols)/2);
          VPIXELSOFF((medias.maxrows/enlarge - rows)/2);
      }
  }
+ 
